﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace sy4_1
{
    public partial class Form1 : Form
    {
        private Form2 f2 = new Form2(); // 全局实例化Form2
        private string nowPath = ""; // 保存当前文件路径

        public Form1()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            f2.ShowDialog();

            if (f2.listBox1.Items.Count > 0)
            {
                nowPath = "";
                textBox1.Text = "以下是您的计算机配置清单：\r\n";
                foreach (var item in f2.listBox1.Items)
                {
                    textBox1.Text += item.ToString() + "\r\n";
                }
            }
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "文本文件|*.txt|所有文件|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                nowPath = ofd.FileName;
                textBox1.Text = File.ReadAllText(nowPath, System.Text.Encoding.Default);
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(nowPath))
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "文本文件|*.txt";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    nowPath = sfd.FileName;
                }
                else
                {
                    return;
                }
            }

            File.WriteAllText(nowPath, textBox1.Text, System.Text.Encoding.Default);
        }


    }
}
